import react from '@vitejs/plugin-react';
import { resolve } from 'path';
import { ConfigEnv, defineConfig } from 'vite';
import { viteStaticCopy } from 'vite-plugin-static-copy';
import svgr from "vite-plugin-svgr";
const { NODE_ENV } = process.env
const release_string_definitions = {
  __MODE__: NODE_ENV,
  __BUILD_TIMESTAMP__: '' + Date.now(),
  __ENV__: 'release',
  __USER_CENTER_SSO_ORIGIN__: "https://user.niushibang.cn",
  __API_URL_BASE__: "https://b-api.niushibang.net",
  __API_URL_GOLTACH__: "https://b-api.niushibang.net/goltach",
  __SSO_DOMAIN_ID__: '25',
}
const stage_string_definitions = {
  ...release_string_definitions,
  __ENV__: 'stage',
  __USER_CENTER_SSO_ORIGIN__: "https://dev-user.niushibang.net",
  __API_URL_BASE__: "https://b-api-dev.niushibang.net",
  __API_URL_GOLTACH__: "https://b-api-dev.niushibang.net/goltach",
}
const local_string_definitions = {
  ...stage_string_definitions,
  __API_URL_BASE__: "http://127.0.0.1:8170",
  __API_URL_GOLTACH__: "http://127.0.0.1:8170",
}

const definitions = {
  release: process_string_definition(release_string_definitions),
  stage: process_string_definition(stage_string_definitions),
  local_api: process_string_definition(local_string_definitions),
}
function process_string_definition<T>(v: T): T {
  if (typeof v === 'string')
    return JSON.stringify(v) as T;
  if (typeof v !== 'object')
    return v
  for (var i in v)
    v[i] = process_string_definition(v[i]);
  return v;
}

export default defineConfig((config_env: ConfigEnv) => {
  const definition = definitions[config_env.mode] || definitions.stage
  return {
    plugins: [
      viteStaticCopy({
        targets: [{
          src: '../CHANGELOG.md',
          dest: './',
        }]
      }),
      react(),
      svgr({ include: "**/*.svg?react", })
    ],
    resolve: {
      alias: {
        '@': resolve(__dirname, 'src'),
        '@native': resolve(__dirname, 'wailsjs/go/main/App'),
        '@runtime': resolve(__dirname, 'wailsjs/runtime/runtime'),
      },
    },
    define: {
      ...definition
    }
  }
})